package com.project.controller;

import com.project.bean.ShopCarBean;
import com.project.bean.UserBean;
import com.project.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lyl
 * description:购物车
 */
@RestController
@RequestMapping("shopCar")
public class ShopCarController {

    @Autowired
    private IShopCarService iShopCarService;

    @RequestMapping("buy")
    public String buy(int productId, HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
        iShopCarService.add(productId, userBean.getUserId());
        return "ok";
    }

    @RequestMapping("findShopCarByUserId")
    public List<ShopCarBean> findByUser(HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");

        return iShopCarService.findAllByUserId(userBean.getUserId());
    }

    @RequestMapping("del")
    public String del(int productId, HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
        iShopCarService.delProductFromShopCar(productId, userBean.getUserId());
        return "ok";
    }

    @RequestMapping("update")
    public String update(int shopCarId, int num) {
        iShopCarService.updateProductNum(shopCarId, num);
        return "ok";
    }

}
